package com.apa7.keepaccounts.controller;

import com.alibaba.fastjson.JSONObject;
import com.apa7.keepaccounts.dto.GetUserInfoRequest;
import com.apa7.keepaccounts.dto.ReturnData;
import com.apa7.keepaccounts.entity.User;
import com.apa7.keepaccounts.service.LoginService;
import com.apa7.keepaccounts.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 注册一个匿名用户
     * @return ReturnData
     */
    @RequestMapping("/registerAnonymous")
    public ReturnData registerAnonymous(){
        Map<String,Object> returnData=new HashMap<>();
        User user=loginService.registerAnonymous();
        returnData.put("key",user.getLoginKey());

        return new ReturnData(0,returnData);
    }

    /**
     * 注册或者更新一个已有用户
     * @return ReturnData
     */
    @RequestMapping("/setUser")
    public ReturnData setUser(){
        JSONObject getUserInfoRequest=Tool.getJsonOject();
        String key=loginService.setUserInfo(getUserInfoRequest.getString("code"),getUserInfoRequest);
        if (key==null||key.equals("")){
            return new ReturnData("获取错误");
        }

        return new ReturnData(0,key);
    }

    /*@RequestMapping("/setCode")
    public ReturnData setCode(){
        JSONObject data=Tool.getJsonOject(request);
        String code=data.getString("code");
        if(code==null||code.equals(""))
            return new ReturnData("参数错误！~~");

        Map<String,Object> returnData=new HashMap<>();
        returnData.put("key",loginService.setCode(code));
        return new ReturnData(0,returnData);
    }*/
}
